########################################################################
#
#	FileName			: env_setter.sh
#	Author				: Zhao Xin
#	CreateTime			: 2017-01-05 03:16:16 AM
#	VIM					: ts=4, sw=4
#	LastModFresh		: 2017-01-05 03:16:16 AM
#
#######################################################################

#!/bin/sh

# OutPut color
Blank="\033[0m"
Red="\033[31m"
Blue="\033[34m"
White="\033[37;1m"
Green="\033[92m"

# Variables
extracted=0								# If has extracted.
scriptSetted=0							# If scripts have been setted.
curPath=`pwd`							# Which dir execute this script.
Buildpath=$(pwd)/.tmp_install_dir		# Temporary make dir.
TarBall=env_setter_pack.tar.gz			# Tarball's name.
_prefix=/usr							# .configure --prefix=/usr
_vimPath=$HOME/.vim						# Current user's vim runtime path.
_vimrc=$HOME/.vimrc						# Current user's vim config.
_profile=$HOME/.bash_profile			# Current user's profile.
YcmPath=$_vimPath/bundle				# Where put the ycm.
YumPath=/etc/yum.repos.d				# Yum repos' path.
gdbInitConf=$HOME/.gdbinit				# GDB initialization configuration file.

function print_start ()
{
	echo ""
	echo -e "${Blue}${1} Start${Blank}"
}

function print_done ()
{
	echo -e "${Blue}${1} Done${Blank}"
	echo ""
}

function check_last_cmd_ok ()
{
	# '$?' is the result of last cmd.
	if [ $? -ne 0 ] 
	then
		echo -e "${Red}Last cmd runs fail! Quit${Blank}" 
		rm -rf $Buildpath
		cd $curPath
		exit $?
	fi
}

function extract_tar ()
{
	# If already extracted, quit.
	if [ $extracted -eq 1 ]; then
		return 0
	fi
	print_start "Extract TarBall"
	rm -rf $Buildpath
	mkdir -p $Buildpath
	sed -n -e '1,/^exit 0$/!p' $0 > "$Buildpath/$TarBall" 2>/dev/null
	tar -xzvf $Buildpath/$TarBall -C $Buildpath/
	check_last_cmd_ok
	extracted=1
	print_done "Extract tarball"
}

function clean_up ()
{
	# If not extracted, quit.
	if [ $extracted -eq 0 ]; then
		return 0
	fi
	print_start "Clean up Extracted Staff"
	rm -rf $Buildpath
	extracted=0
	print_done "Clean up"
}

function set_yum ()
{
	print_start "Set Yum Repo"
	extract_tar
	cd $Buildpath/config
	mkdir -p $YumPath
	cp CentOS-Base.repo $YumPath
	cd $YumPath
	yum -y install epel-release && rpm -Uvh https://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
	yum clean all && yum makecache && yum -y update
	cd $curPath
	print_done "Set yum"
}

function set_script ()
{
	print_start "Set Script"
	extract_tar
	cd $Buildpath/script
	mkdir -p $HOME/bin
	cp mktest.sh shtest.sh asmtest.sh prepareProject.sh git_info.sh svn_diff.sh $HOME/bin
	cd $Buildpath/config
	mv $_vimrc ${_vimrc}_blk
	cp .vimrc $_vimrc
	# Selfdefine test scripts.
	echo "alias mktest=\". mktest.sh\"" >> $_profile
	echo "alias shtest=\". shtest.sh\"" >> $_profile
	echo "alias asmtest=\". asmtest.sh\"" >> $_profile
	echo "alias git_info=\". git_info.sh\"" >> $_profile
	# Set gdb disassembly language to intel asm.
	echo "set disassembly-flavor intel" > $gdbInitConf
	cd $curPath
	scriptSetted=1
	print_done "Set Script"
}

function install_ycm ()
{
	print_start "Start Install YouCompleteMe"
	
	# Install python 2.7.
	print_start "Install or Update Python"
	yum install -y python
	check_last_cmd_ok
	yum install -y python-devel
	check_last_cmd_ok
	print_done

	# Install cscope.
	print_start "Install Cscope"
	yum install -y cscope
	check_last_cmd_ok
	print_done

	extract_tar

	# Check if gcc installed, if not, install it.
	if [ "`gcc --version`" == "" ]; then
		echo -e "${Red}Gcc is not installed, install gcc and g++${Blank}"
		yum install -y gcc
		yum install -y gcc-c++
		yum install -y libstdc++-devel
		print_done "Gcc and G++ installation"
	fi

	# cmake install.
	cmake_version=`cmake --version`
	cmake_version_arr=($cmake_version)
	if [[ "$cmake_version" == "" || ${cmake_version_arr[2]} < "3.6" ]]; then
		print_start "Install 3.6 version CMake"
		yum erase -y cmake
		cd $Buildpath/cmake-3.6.3
		./configure --prefix=$_prefix
		check_last_cmd_ok
		gmake && gmake install
		check_last_cmd_ok
		cd ..
		print_done
	fi

	# Ycm install.
	print_start "Install YouCompleteMe"
	mkdir -p $YcmPath
	mv YouCompleteMe $YcmPath
	cd $YcmPath/YouCompleteMe
	mkdir build
	cd build
	#cmake -G "Unix Makefiles" -DEXTERNAL_LIBCLANG_PATH=/usr/lib/libclang.so $YcmPath/YouCompleteMe/third_party/ycmd/cpp
	cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=$Buildpath/llvm_clang_binary . $YcmPath/YouCompleteMe/third_party/ycmd/cpp
	check_last_cmd_ok
	cmake --build . --target ycm_core --config Release
	check_last_cmd_ok
	cd ..
	cp -r ./third_party/ $_vimPath/
	cp -r ./python/ $_vimPath/
	cp -r ./plugin/ $_vimPath/
	cp -r ./autoload/ $_vimPath/
	# I have no way to build doc's tag outside of vim, so have to do it manually
	cp -r ./doc/ $_vimPath/
	cd $Buildpath/config
	cp .ycm_extra_conf.py $_vimPath/
	if [ $scriptSetted -eq 0 ]; then
		echo -e "${Blue}Set .vimrc for ycm${Blank}"
		echo "" >> $_vimrc
		echo "\"build helptags for plugins every time" >> $_vimrc
		echo ":helptags $_vimPath/doc" >> $_vimrc
		echo "" >> $_vimrc
		echo "\"[---YouCompleteMe settings---]" >> $_vimrc
		echo "let g:ycm_key_list_select_completion = []" >> $_vimrc
		echo "let g:ycm_key_list_previous_completion = []" >> $_vimrc
		echo "let g:ycm_confirm_extra_conf = 0" >> $_vimrc
		echo "let g:ycm_goto_buffer_command = 'horizontal-split'" >> $_vimrc
		echo "let g:ycm_warning_symbol = '>'" >> $_vimrc
		echo "let g:ycm_global_ycm_extra_conf = '$_vimPath/.ycm_extra_conf.py'" >> $_vimrc
		print_done ".vimrc Setting"
	fi
	print_done "YouCompleteMe Installation"
}

function print_help ()
{
	echo -e "${Green}Usage:"
	echo "	yum: Set yum repo."
	echo "	script: Add scripts(mktest.sh, mkshell.sh, prepareProject.sh, .vimrc)"
	echo "	ycm: Install YouCompleteMe plugin to local host(need install python2.7, cmake3.6 first, set .ycm_extra_conf)"
	echo -e "	all or empty: Install all above.${Blank}"
}

#if [[ $# -eq 0 || $# -eq 1 && $1 = "all" ]]; then
#	set_yum
#	set_script
#	install_ycm
#elif [ $# -eq 1 ]; then
if [ $# -eq 1 ]; then
	if [ "$1" = "all" ]; then
		set_yum
		set_script
		install_ycm
	fi
	if [ "$1" = "yum" ]; then
		set_yum
	fi
	if [ "$1" = "script" ]; then
		set_script
	fi
	if [ "$1" = "ycm" ]; then
		install_ycm
	fi
else
	print_help
fi

clean_up

exit 0
